package fr.epsi.spring1.service.impl;

import java.util.List;

import fr.epsi.spring1.dao.ArticleDao;
import fr.epsi.spring1.domain.Article;
import fr.epsi.spring1.service.ArticleManager;

public class ArticleManagerImpl implements ArticleManager {

	private ArticleDao articleDao;

	
	@Override
	public List<Article> rechercher(String reference) {
		List<Article> listOfArticles = articleDao.select(reference);
		return listOfArticles;
	}

	@Override
	public boolean insert(Article article) {
		if (articleDao.getArticle(article.getReference()) == null) {
			if (article.getPrix() != null && article.getPrix() > 0 
					&& article.getReference() != null) {
				return articleDao.insert(article);
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public boolean update(Article article) {
		Article existingArticle = articleDao.getArticle(article.getReference());
		if (existingArticle != null && !existingArticle.getEpuise()) {
			if (article.getPrix() != null && article.getPrix() > 0 
					&& article.getReference() != null) {
				articleDao.update(article);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	@Override
	public void retirerDeLaVente(Article article) {
		Article existingArticle = articleDao.getArticle(article.getReference());
		if (existingArticle != null) {
			existingArticle.setEpuise(true);
			articleDao.update(existingArticle);
		}
	}

	@Override
	public Double getNbArticles() {
		return articleDao.getNbArticles();
	}

}
